﻿Imports System.Xml.Linq

Public NotInheritable Class PluginSetting

    Private Const SettingFile As String = "Setting.xml"

    Private Shared _languages As IList(Of LanguageItem)
    Private Shared _coreJSFile As String
    Private Shared _cssFile As String

    Public Shared ReadOnly Property Languages() As IList(Of LanguageItem)
        Get
            Return _languages
        End Get
    End Property

    Public Shared ReadOnly Property CoreJSFile() As String
        Get
            Return _coreJSFile
        End Get
    End Property

    Public Shared ReadOnly Property CssFile() As String
        Get
            Return _cssFile
        End Get
    End Property

    Public NotInheritable Class LanguageItem
        Private _name As String
        Private _jsFile As String
        Private _className As String

        Public ReadOnly Property Name() As String
            Get
                Return _name
            End Get
        End Property

        Public ReadOnly Property JSFile() As String
            Get
                Return _jsFile
            End Get
        End Property

        Public ReadOnly Property ClassName() As String
            Get
                Return _className
            End Get
        End Property

        Public Overrides Function ToString() As String
            Return _name
        End Function

        Sub New(ByVal name As String, ByVal jsFile As String, ByVal className As String)
            _name = name
            _jsFile = jsFile
            _className = className
        End Sub
    End Class

    Shared Sub New()

        Dim file = System.IO.Path.Combine( _
            System.IO.Path.GetDirectoryName(Reflection.Assembly.GetAssembly(GetType(PluginSetting)).Location), SettingFile)
        Dim doc = XDocument.Load(file)

        _coreJSFile = doc.<setting>.<corejsfile>.Value
        _cssFile = doc.<setting>.<cssfile>.Value

        _languages = New List(Of LanguageItem)
        For Each e In doc...<language>
            _languages.Add(New LanguageItem(e.<name>.Value, e.<jsfile>.Value, e.<class>.Value))
        Next

    End Sub

    Private Shared CoreJsReadText As String
    Public Shared Function ReadCoreFile() As String

        If Not String.IsNullOrEmpty(CoreJsReadText) Then
            Return CoreJsReadText
        End If

        Dim fs As System.IO.FileStream = Nothing
        Dim sr As System.IO.StreamReader = Nothing
        Try
            Dim file = System.IO.Path.Combine( _
                System.IO.Path.GetDirectoryName(Reflection.Assembly.GetAssembly(GetType(PluginSetting)).Location), CoreJSFile)

            fs = New System.IO.FileStream(file, IO.FileMode.Open, IO.FileAccess.Read)
            sr = New System.IO.StreamReader(fs)

            CoreJsReadText = sr.ReadToEnd

        Catch
            ' Ignore
            CoreJsReadText = ""
        Finally
            If sr IsNot Nothing Then
                sr.Close()
            End If
            If fs IsNot Nothing Then
                fs.Close()
            End If
        End Try

        Return CoreJsReadText
    End Function

    Public Shared Function ReadLanguageJSFile(ByVal language As String) As String

        Dim fs As System.IO.FileStream = Nothing
        Dim sr As System.IO.StreamReader = Nothing
        Try
            Dim js = (From l In _languages Where language = l.ClassName).Single.JSFile
            Dim file = System.IO.Path.Combine( _
                System.IO.Path.GetDirectoryName(Reflection.Assembly.GetAssembly(GetType(PluginSetting)).Location), js)

            fs = New System.IO.FileStream(file, IO.FileMode.Open, IO.FileAccess.Read)
            sr = New System.IO.StreamReader(fs)

            Return sr.ReadToEnd

        Catch
            ' Ignore
        Finally
            If sr IsNot Nothing Then
                sr.Close()
            End If
            If fs IsNot Nothing Then
                fs.Close()
            End If
        End Try

        Return ""

    End Function

End Class
